<?php
session_start();
error_reporting(0);
// phpinfo();
include('./config.php');
include('./db/Db.php');
include('./db/Sql.php');

class Login extends Sql
{
    protected $table = "user_tbl";
}
class API extends Sql
{
    protected $table = "nav_tbl";
}
$action = isset($_GET['action']) ? $_GET['action'] : 0;
$names = isset($_POST['names']) ? $_POST['names'] : "";
$urls = isset($_POST['urls']) ? $_POST['urls'] : "";
$uid = isset($_SESSION['uid']) ? $_SESSION['uid'] : "";
$id = isset($_POST['id']) ? $_POST['id'] : "";

switch ($action) {
    case 1:
        login();
        break;
    case 2:
        logout();
        break;
    case 3:
        getNavByUid($uid);
        break;
    case 4:
        addItem($names, $urls, $uid);
        break;
    case 5:
        getNowUser();
        break;
    case 6:
        deleteItem($id,$uid);
        break;
    default:
    $s['msg'] = "无操作";
    $s['code'] = 0;
    echo json_encode($s,true);
}
function login()
{
    $s = ["code" => 0, "msg" => "ERROR"];
    if (isset($_POST['uname']) && isset($_POST['upasswd'])) {
        if ($_POST['uname'] == null || $_POST['upasswd'] == null) {
            $s['msg'] = "用户名或密码为空";
            $s['code'] = 0;
            echo json_encode($s,true);
            return;
        }
        $login = new Login();
        try {
            $ans = $login->where(["username = :uname"], [':uname' => $_POST["uname"]])->fetchAll();
        } catch (Exception $e) {
            $s['msg'] = "ERROR";
            $s['code'] = 0;
            echo (json_encode($s,true));
            return;
        }
        if (empty($ans) || strcmp($ans[0]['passwd'], md5($_POST["upasswd"]))) {
            $s['msg'] = "用户名或密码错误";
            $s['code'] = 0;
            echo (json_encode($s,true));
            return;
        } else if (isset($_SESSION['is_login']) && $_SESSION['is_login'] == true) {
            $s['msg'] = "用户已登陆,请退出登陆后再试!";
            $s['code'] = 0;
            echo (json_encode($s,true));
            return;
        } else {
            $s['msg'] = "登陆成功!";
            $s['code'] = 1;
            $_SESSION['uid'] = $ans[0]['userId'];
            $_SESSION['name'] = $ans[0]['username'];
            $_SESSION['is_login'] = true;
            echo (json_encode($s,true));
            return;
        }
    }
    echo (json_encode($s,true));
    return;
}

function logout()
{
    $s = ["code" => 1, "msg" => "已经退出登陆"];
    $_SESSION['is_login'] = false;
    unset($_SESSION['uid']);
    unset($_SESSION['is_login']);         
    unset($_SESSION['name']);
    echo (json_encode($s,true));
    return;
}

function getNavByUid($uid)
{
    $s = ["code" => 0, "msg" => "ERROR"];
    $nav = new API();
    try {
        $ans = $nav->where(["userId = :userId"], [':userId' => $uid])->fetchAll();
    } catch (Exception $e) {
        $s['msg'] = "ERROR";
        $s['code'] = 0;
        echo (json_encode($s,true));
        return;
    }  
    $s['code'] = 1;
    $s['data'] = $ans;
    $s['msg'] = "successful";
    echo (json_encode($s,true));
    return;
}

function addItem($name, $url, $uid)
{
    $s = ["code" => 0, "msg" => "ERROR"];
    $nav = new API();
    if (isset($name) && isset($url) && isset($_SESSION['is_login']) && $_SESSION['is_login'] == true) {
        if ($name == null || $url == null) {
            $s['msg'] = "name或者url为空";
            $s['code'] = 0;
            echo json_encode($s,true);
            return;
        }
        else {
            $data = ['name' => $name, 'url' => $url, 'userId' => $uid];
            try {
                $ans = $nav->add($data);
            } catch (Exception $e) {
                $s['msg'] = "ERROR";
                $s['code'] = 0;
                echo (json_encode($s,true));
                return;
            }
            $s['msg'] = "添加成功!";
            $s['code'] = 1;
            echo (json_encode($s,true));
            return;
        }
    } 
    $s['msg'] = "需要登陆才能添加";
    $s['code'] = 0;
    echo json_encode($s,true);
    return;
}

function getNowUser(){
    $s = ["code" => 0, "msg" => "ERROR"];
    if(isset($_SESSION['is_login']) && $_SESSION['is_login'] == true){
        $login = new Login();
        try {
            $ans = $login->where(["userId = :userId"], [':userId' => $_SESSION['uid']])->fetchAll();
        } catch (Exception $e) {
            $s['msg'] = "ERROR";
            $s['code'] = 0;
            echo (json_encode($s,true));
            return;
        }
        $s['data'] = $ans;
        $s['msg'] = "fetch successful";
        $s['code'] = 1;
        echo (json_encode($s,true));
        return;
    }else{
        $s['msg'] = "fetch error";
        $s['code'] = 0;
        echo (json_encode($s,true));
        return;
    }
}

function deleteItem($id,$uid){
    $s = ["code" => 0, "msg" => "ERROR"];
    $nav = new API();
    if (isset($id)&& isset($_SESSION['is_login']) && $_SESSION['is_login'] == true) {
        if ($id == null) {
            $s['msg'] = "id null";
            $s['code'] = 0;
            echo json_encode($s,true);
            return;
        }
        else {
            try {
                $ans = $nav->where(["id = :id and userId = :userId"], [':id' => $id,':userId' => $uid])->delete();
            } catch (Exception $e) {
                $s['msg'] = "ERROR";
                $s['code'] = 0;
                echo (json_encode($s,true));
                return;
            }
            if($ans==1){
                $s['msg'] = "删除成功!";
                $s['data'] = $ans;
                $s['code'] = 1;
                echo (json_encode($s,true));
                return;
            }else{
                $s['msg'] = "删除失败!";
                $s['data'] = $ans;
                $s['code'] = 0;
                echo (json_encode($s,true));
                return;
            }

        }
    }else{
        $s['msg'] = "需要登陆或着指定id才能删除";
        $s['code'] = 0;
        echo json_encode($s,true);
        return;
    }

}